package sf.database.jpa;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sf.database.dao.DBClient;

import javax.persistence.Cache;
import javax.persistence.EntityGraph;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnitUtil;
import javax.persistence.Query;
import javax.persistence.SynchronizationType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.metamodel.Metamodel;
import javax.sql.DataSource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class JpaEntityManagerFactory implements EntityManagerFactory {
    private static Logger log = LoggerFactory.getLogger(JpaEntityManagerFactory.class);

    protected DataSource dataSource;

    private DBClient db;
    private final Map<String, Object> properties = new HashMap<String,Object>();

    public JpaEntityManagerFactory(DBClient db) {
        this.db = db;
        properties.put("javax.persistence.nonJtaDataSource","");
    }


    @Override
    public EntityManager createEntityManager() {
        return createEntityManager(null, Collections.EMPTY_MAP);
    }

    @Override
    public EntityManager createEntityManager(Map map) {
        return createEntityManager(SynchronizationType.SYNCHRONIZED, map);
    }

    @Override
    public EntityManager createEntityManager(SynchronizationType synchronizationType) {
        return createEntityManager(synchronizationType, Collections.EMPTY_MAP);
    }

    @Override
    public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) {
        EntityManager result = new JpaEntityManagerImpl(this, map);
        log.debug("[JPA DEBUG]:creating EntityManager:{} at {}", result, Thread.currentThread());
        return result;
    }

    @Override
    public CriteriaBuilder getCriteriaBuilder() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Metamodel getMetamodel() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isOpen() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void close() {
        // TODO Auto-generated method stub

    }

    @Override
    public Map<String, Object> getProperties() {
        return properties;
    }

    @Override
    public Cache getCache() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public PersistenceUnitUtil getPersistenceUnitUtil() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void addNamedQuery(String name, Query query) {
        // TODO Auto-generated method stub

    }

    @Override
    public <T> T unwrap(Class<T> cls) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public <T> void addNamedEntityGraph(String graphName, EntityGraph<T> entityGraph) {
        // TODO Auto-generated method stub

    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public DBClient getDb() {
        return db;
    }
}
